package com.maysev.myjob_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maysev.myjob_backend.common.R;
import com.maysev.myjob_backend.entity.PerAuth;
import com.maysev.myjob_backend.service.PerAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 五七
 * @create 2023-03-30 15:34
 * @description:
 */
@RestController
@Slf4j
@CrossOrigin("http://localhost:8082")
@RequestMapping("/perAuth")
public class PerAuthController {

    @Autowired
    private PerAuthService perAuthService;


    /**
     * 查询用户实名认证信息
     * @param userId
     * @return
     */
    @GetMapping
    private R<PerAuth> getPerAuthInfo(@RequestParam Long userId){
        //1. 根据用户id查询用户查询
        LambdaQueryWrapper<PerAuth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PerAuth::getUserId,userId);
        PerAuth per = perAuthService.getOne(queryWrapper);
        //2. 未查到,则返回错误信息
        if(per == null){
            //"0":表示未认证
            return R.error("未认证");
        }
        //3. 查询到,返回用户信息

        return R.success(per);
    }

    /**
     * 将实名认证信息保存到数据库,
     * @param perAuth
     * @return
     */
    @PostMapping
    private R<String> submitPerAuthInfo(@RequestBody PerAuth perAuth){
        log.info(perAuth.toString());
        //1.获取提交的认证信息
        LambdaQueryWrapper<PerAuth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PerAuth::getUserId,perAuth.getUserId());

        PerAuth one = perAuthService.getOne(queryWrapper);
        //2.判断是添加还是修改
        if (one == null){
            //添加
        perAuthService.save(perAuth);

        }
        //修改
        perAuthService.updateById(perAuth);

        return R.success("提交成功");
    }


}
